package controls;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import util.LogTable;
import util.Name;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import dao.Database;

/**
 * Servlet implementation class InitList
 */
@WebServlet("/InitList")
public class InitList extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Connection conn = null;
	private LogTable logTable = null;
	
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public InitList() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String action = request.getParameter("action");
		
		
		logTable = (LogTable) request.getServletContext().getAttribute("logTable");
		
		Database db = new Database();
		try {
			conn = db.getConnection();
			if(action.equals("getCountry")){
				String countryList = getCountryList(conn,logTable);
				out.println(countryList);
			}
			else if(action.equals("getCity")){
				String country = request.getParameter("selCountry");
				String cityList = getCityList(conn,country,logTable);
				out.println(cityList);	
			}
			else if(action.equals("getLevel0")){
				String level0List = getLevel0List(conn,logTable);
				out.println(level0List);
			}
			else if(action.equals("getLevel1")){
				String level0 = request.getParameter("selLevel0");
				String level1List = getLevel1List(conn,level0,logTable);
				out.println(level1List);
				
			}
			else if(action.equals("getLevel2")){
				String level0 = request.getParameter("selLevel0");
				String level1 = request.getParameter("selLevel1");
				out.println(getLevel2List(conn,level0,level1,logTable)); 
			}
			else if(action.equals("getYear")){
				String yearList = getyearList(conn,logTable);
				out.println(yearList);
			}
			else if(action.equals("getMonth")){
				String year = request.getParameter("selYear");
				String monthList = getmonthList(conn,year,logTable);
				out.println(monthList);
			}
			else if(action.equals("getDay")){
				String year = request.getParameter("selYear");
				String month = request.getParameter("selMonth");
				String dayList = getdayList(conn,year,month,logTable);
				out.println(dayList);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			out.close();
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	private String getCountryList(Connection conn,LogTable logTable) throws Exception{
		
	    ArrayList <String> cList = logTable.countryList;
		Gson gson = new Gson();
		String data = gson.toJson(cList);
		System.out.println("country:"+data);
		return "{\"names\":"+data+"}";
		 
	}
	
	private String getCityList(Connection conn,String country,LogTable logtable) throws Exception{
				
		String key = Name.country+":"+country;
		ArrayList <String> cityList = logtable.relationMap.get(key); 
		
		Gson gson = new Gson();
		String data = gson.toJson(cityList);
		System.out.println(data);
		return "{\"names\":"+data+"}";
	}
	
	private String getLevel0List(Connection conn,LogTable logTable) throws Exception{
	
		ArrayList <String> level0List = logTable.level0List;
		
		Gson gson = new Gson();
		String data = gson.toJson(level0List);
		System.out.println("Level0:"+data);
		return "{\"names\":"+data+"}";
	}
	
	private String getLevel1List(Connection conn,String level0,LogTable logTable) throws Exception{
//		String sqlStr = "select distinct checkintable.level1Label from cube.checkintable where checkintable.level0Label='"+level0+"'";
//		PreparedStatement stmt = conn.prepareStatement(sqlStr);
//		ResultSet rs = stmt.executeQuery();
//		ArrayList <String> level1List = new ArrayList<String>();
//		
//		while(rs.next()){
//			String level1 = rs.getString(1).trim();
//			level1List.add(level1);
//		}
//		level1List.remove(new String("empty"));
//		level1List.add(0, "none");
		Gson gson = new Gson();
		String key = Name.level0+":"+level0;
		ArrayList <String> level1List = logTable.relationMap.get(key);
		String data = gson.toJson(level1List);
		System.out.println(data);
		return "{\"names\":"+data+"}";
	}
	
	private String getLevel2List(Connection conn,String level0,String level1,LogTable logTable) throws Exception{
//		String sqlStr = "select distinct checkintable.level2Label from cube.checkintable where checkintable.level0Label='"+level0+"' and checkintable.level1Label='"+level1+"'";
//		PreparedStatement stmt = conn.prepareStatement(sqlStr);
//		ResultSet rs = stmt.executeQuery();
//		ArrayList <String> level2List = new ArrayList<String>();
//		
//		while(rs.next()){
//			String level2 = rs.getString(1).trim();
//			level2List.add(level2);
//		}
//		level2List.remove(new String("empty"));
//		level2List.add(0, "none");
		String key = Name.level0 + ":"+level0+":"+Name.level1+":"+level1;
		ArrayList <String> level2List = logTable.relationMap.get(key);

		Gson gson = new Gson();
		String data = gson.toJson(level2List);
		System.out.println(data);
		return "{\"names\":"+data+"}";
	}
	
	private String getyearList(Connection conn,LogTable logTable) throws Exception{
//		String sqlStr = "select distinct cube.datetable.year from cube.datetable";
//		PreparedStatement stmt = conn.prepareStatement(sqlStr);
//		ResultSet rs = stmt.executeQuery();
//		ArrayList <String> yearList = new ArrayList<String>();
//		
//		while(rs.next()){
//			String level2 = rs.getString(1).trim();
//			yearList.add(level2);
//		}
//		Collections.sort(yearList);
//		yearList.add(0, "none");

		ArrayList yearList = logTable.yearList;
		Gson gson = new Gson();
		String data = gson.toJson(yearList);
		System.out.println(data);
		return "{\"names\":"+data+"}";
	}
	
	private String getmonthList(Connection conn,String year,LogTable logTable) throws Exception{
//		String sqlStr = "select distinct cube.datetable.month from cube.datetable where datetable.year='"+year+"'";
//		PreparedStatement stmt = conn.prepareStatement(sqlStr);
//		ResultSet rs = stmt.executeQuery();
//		ArrayList <String> monthList = new ArrayList<String>();
//		
//		while(rs.next()){
//			String level2 = rs.getString(1).trim();
//			monthList.add(level2);
//		}
//		Collections.sort(monthList);
//		monthList.add(0, "none");
		
		String key = Name.year + ":" + year;
		ArrayList monthList = logTable.relationMap.get(key);
		
		Gson gson = new Gson();
		String data = gson.toJson(monthList);
		System.out.println(data);
		return "{\"names\":"+data+"}";
	}
	
	private String getdayList(Connection conn,String year,String month,LogTable logTable) throws Exception{
//		String sqlStr = "select distinct cube.datetable.day from cube.datetable where datetable.year='"+year+"' and datetable.month='"+month+"'";
//		PreparedStatement stmt = conn.prepareStatement(sqlStr);
//		ResultSet rs = stmt.executeQuery();
//		ArrayList <String> dayList = new ArrayList<String>();
//		
//		while(rs.next()){
//			String day = rs.getString(1).trim();
//			dayList.add(day);
//		}
//		Collections.sort(dayList);
//		dayList.add(0, "none");
		
		String key = Name.year+":"+year+":"+Name.month+":"+month;
		ArrayList <String> dayList = logTable.relationMap.get(key);

		Gson gson = new Gson();
		String data = gson.toJson(dayList);
		System.out.println(data);
		return "{\"names\":"+data+"}";
	}	
	
}


